//
//  UIButton+ImageWithLable.m
//  QYPlayerController
//
//  Created by lcb on 5/18/15.
//  Copyright (c) 2015 Zhang Xiaoyong. All rights reserved.
//

#import "UIButton+ImageWithLable.h"
#define UIColorRGB1(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
@implementation UIButton(ImageWithLable)

- (void) setUDImage:(UIImage *)image withTitle:(NSString *)title forState:(UIControlState)stateType {
    //UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right)
    
//    CGSize titleSize = [title sizeWithFont:[UIFont systemFontOfSize:12.0f]];
    CGSize titleSize = [title boundingRectWithSize:CGSizeMake(MAXFLOAT,0.0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12.0f]}context:nil].size;
    
    
    [self.imageView setContentMode:UIViewContentModeCenter];
    [self setImageEdgeInsets:UIEdgeInsetsMake(-8.0,
                                              0.0,
                                              0.0,
                                              -titleSize.width)];
    [self setImage:image forState:stateType];
    
    [self.titleLabel setContentMode:UIViewContentModeCenter];
    [self.titleLabel setBackgroundColor:[UIColor clearColor]];
    [self.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Light" size:12]];
    [self.titleLabel setTextColor:[UIColor darkGrayColor]];
    [self setTitleEdgeInsets:UIEdgeInsetsMake(30.0,
                                              -image.size.width,
                                              0.0,
                                              0.0)];
    [self setTitle:title forState:stateType];
}
- (void) setLRImage:(UIImage *)image withTitle:(NSString *)title forState:(UIControlState)stateType {
    //UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right)
    
    [self.imageView setContentMode:UIViewContentModeCenter];
    
    [self setImageEdgeInsets:UIEdgeInsetsMake(0.0,
                                              -((self.frame.size.width)/2+(image.size.width)/2),
                                              0.0,
                                              0.0)];
    [self setImage:image forState:stateType];
    
    [self.titleLabel setContentMode:UIViewContentModeCenter];
    [self.titleLabel setBackgroundColor:[UIColor clearColor]];
    [self.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Light" size:12]];
    [self.titleLabel setTextColor:[UIColor darkGrayColor]];
    [self setTitleEdgeInsets:UIEdgeInsetsMake(2.0,
                                              -(self.frame.size.width)/2,
                                              0.0,
                                              0.0)];
    [self setTitle:title forState:stateType];
}

@end
